home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / satellit / pacdoc / blogdisp.c < prev    next >
C/C++ Source or Header  |  1993-05-16  |  10KB  |  302 lines

  1. /*
  2.         Display program for BL files, which record broadcast server activity.
  3.         This code is not particularly portable. It was written for Microsoft C under DOS.
  4.         It will also compile under Quick C for windows, and the unixtime module sorts
  5.         out the problem that ANSI standard time is not the same as good old unix time.
  6.  
  7.         fFixed -     This flag is FALSE for early UoSAT-3 and UoSAT-5 BL files because there was
  8.                           a bug in the onboard logging task wich caused incorrect counts in the
  9.                           entries for overwritten requests and un-fresh requests.
  10.  
  11.         Original: Jeff Ward, Surrey Satellite Technology, Ltd.
  12. */
  13. /*
  14.         History:
  15.         19 April 1993
  16.         Added ability to read the version from the log file if the first entry
  17.         in the log file has length 2. Otherwise version is assumed to be
  18.         version 1.
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "unixtime.h"
  24. #include "blogdisp.h"
  25.  
  26. #define MAGIC 25569.0
  27.  
  28. struct STAT_STRUCT stats;
  29. struct STAT_STRUCT statDaily;
  30. int fBytemode = FALSE;                                                    /* Display byte counts */
  31. int fCmdmode = FALSE;                                                        /* Display command counts */
  32. int fDirmode = FALSE;                                                        /* Display dir counts */
  33. int fFixed = TRUE;                              /* Problem with some byte counts */
  34.  
  35. void main(int argc, char * argv[]){
  36.     FILE * pFile;
  37.     time_t tim;
  38.     int len;
  39.     double timSerial;                                                            /* Microsoft time serial no.                 */
  40.     int fNewCSV = FALSE;                                                    /* True when building new CSV file     */
  41.     int fInited = FALSE;                                                    /* Set after reading first record        */
  42.     int version;
  43.  
  44.     /* Struct to store daily totals */
  45.     memset(&statDaily, 0, sizeof(struct STAT_STRUCT));
  46.  
  47.     /* Insufficient args. Show help */
  48.     if (argc<2){
  49.         fprintf(stderr,"blogdisp <blname> [<-b | -c | -d> [<csvname>]]  Broadcast log display program.\n");
  50.         fprintf(stderr, "Version 1.6\n");
  51.         fprintf(stderr,"<blname> is the name of the BL log input file.\n");
  52.         fprintf(stderr,"-b to display byte counters.\n");
  53.         fprintf(stderr,"-c to display command counters.\n");
  54.         fprintf(stderr,"-d to display directory counters.\n");
  55.         fprintf(stderr,"<csvname> is name of a comma separated value output file for spreadsheets.\n");
  56.         exit(1);
  57.     }
  58.     /* Check second arg for display flag */
  59.     if (argc > 2){
  60.         strlwr(argv[2]);
  61.         if (!strcmp(argv[2], "-b"))
  62.             fBytemode = TRUE;
  63.         else if (!strcmp(argv[2], "-c"))
  64.             fCmdmode = TRUE;
  65.         else if (!strcmp(argv[2], "-d"))
  66.             fDirmode = TRUE;
  67.     }
  68.     /* Or default to command counter display */
  69.     else
  70.         fCmdmode = TRUE;
  71.  
  72.   /* First arg supplies input file name */
  73.     pFile = fopen(argv[1], "rb");
  74.     if (pFile){
  75.         version = GetVer(pFile);
  76.         if (version > 0){
  77.  
  78.             while (! feof(pFile)){
  79.                 if (fread(&len, sizeof(int), 1, pFile)) {
  80.                     if (fread(&tim, sizeof(tim), 1, pFile)) {
  81.                         if (!fInited){
  82.                             fInited = TRUE;
  83.                             fFixed = IsNewLog(tim);
  84.                         }
  85.                         if (fread(&stats, len-sizeof(tim), 1, pFile)){
  86.                             process(&tim, &stats, version);
  87.                             sum(&stats, &statDaily, version);
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         else{
  94.             printf("Error reading version ID from log file.\n");
  95.         }
  96.         fclose(pFile);
  97.  
  98.         /*     Possible third arg supplies output file name for comma    */
  99.         /*    separated value output for spreadsheets.                                */
  100.         if (argc == 4){
  101.  
  102.             /* Determine whether this is a new .CSV file or one to        */
  103.             /* append to.                                                                                            */
  104.             pFile = fopen(argv[3], "rt");
  105.             if (pFile == NULL)
  106.                 fNewCSV = TRUE;
  107.             else{
  108.                 fNewCSV = FALSE;
  109.                 fclose(pFile);
  110.             }
  111.  
  112.             pFile = fopen(argv[3], "at");
  113.             if (pFile){
  114.                 /* Put top in .CSV file if new */
  115.                 if (fNewCSV){
  116.                     fprintf(pFile, "date,Start Cmds,Hole Fills,End Cmds,"
  117.                                                  "No -2,No -1,No -3,"
  118.                                                  "Bytes requested,Bytes transmitted,Ticks,Overwritten,"
  119.                                                  "Too old,Ended,Fopen Error,PFH Error,Dir Requests,"
  120.                                                  "Bytes of Dirs Txd,Ticks,Stations Heard,Eof,Long cmds\n");
  121.                 }
  122.                 /* Remove bogus values if not in a fixed log */
  123.                 if (!fFixed){
  124.                     statDaily.nbOverwrite = 0l;
  125.                     statDaily.nbUnfresh = 0l;
  126.                 }
  127.                 timSerial = ((double) tim / (24.0*60.0*60.0)) + MAGIC;
  128.                 fprintf(pFile, "%8.4f,%ld,%ld,%ld,"
  129.                                              "%ld,%ld,%ld,%ld,"
  130.                                              "%ld,%ld,%ld,%ld,"
  131.                                              "%ld,%ld,%ld,%ld,%ld,%ld,%d,%ld,%d\n",
  132.                     timSerial,
  133.                     statDaily.nStartFile,
  134.                     statDaily.nHolefills,
  135.                     statDaily.nEndFile,
  136.                     statDaily.nNoFile,
  137.                     statDaily.nNoRoom,
  138.                     statDaily.nNotOK,
  139.                   statDaily.nbRequested,
  140.                     statDaily.nbTransmitted,
  141.                     statDaily.TicksData,
  142.                     statDaily.nbOverwrite,
  143.                     statDaily.nbUnfresh,
  144.                     statDaily.nbEnd,
  145.                     statDaily.nbFopenErr,
  146.                     statDaily.nbPfhErr,
  147.                     statDaily.nDirReqs,
  148.                     statDaily.nbDirTxd,
  149.                     statDaily.TicksDir,
  150.                     statDaily.nNewStns,
  151.                     statDaily.nbEof,
  152.                     statDaily.nLongFile
  153.                     );
  154.                 fclose(pFile);
  155.             }
  156.         }
  157.     }
  158.     /* No such file as first argument specifies */
  159.     else{
  160.         fprintf(stderr, "blogdisp - Error: input file does not exist.\n");
  161.         exit(2);
  162.     }
  163. }
  164.  
  165. int GetVer(FILE * pFile){
  166.     int ver=-1;
  167.     int len;
  168.     if (fread(&len, sizeof(int), 1, pFile)) {
  169.         if (len == 2) {
  170.             fread(&ver, 2, 1, pFile);
  171.         }
  172.         else {
  173.             ver = 1;
  174.             fseek(pFile, 0L, SEEK_SET);
  175.         }
  176.     }
  177.     return ver;
  178. }
  179. /*-----------------------------------------------*/
  180. /* Add present stats values to the running total */
  181. /*-----------------------------------------------*/
  182. void sum(struct STAT_STRUCT * pStats, struct STAT_STRUCT * pTotal, int version){
  183.     pTotal->nStartFile += pStats->nStartFile;
  184.     pTotal->nHolefills  += pStats->nHolefills;
  185.     pTotal->nEndFile += pStats->nEndFile;
  186.     pTotal->nNoFile += pStats->nNoFile;
  187.     pTotal->nNoRoom += pStats->nNoRoom;
  188.     pTotal->nNotOK += pStats->nNotOK;
  189.   pTotal->nbRequested  += pStats->nbRequested;
  190.     pTotal->nbTransmitted  += pStats->nbTransmitted;
  191.     pTotal->nbOverwrite  += pStats->nbOverwrite;
  192.     pTotal->nbUnfresh  += pStats->nbUnfresh;
  193.     pTotal->nbEnd  += pStats->nbEnd;
  194.     pTotal->nbFopenErr  += pStats->nbFopenErr;
  195.     pTotal->nbPfhErr  += pStats->nbPfhErr;
  196.     pTotal->nDirReqs += pStats->nDirReqs;
  197.     pTotal->nbDirTxd += pStats->nbDirTxd;
  198.     if (version > 1){
  199.         pTotal->TicksDir += pStats->TicksDir;
  200.         pTotal->TicksData += pStats->TicksData;
  201.         pTotal->nNewStns += pStats->nNewStns;
  202.         if (version>2){
  203.             pTotal->nbEof += pStats->nbEof;
  204.             pTotal->nLongFile += pStats->nLongFile;
  205.         }
  206.     }
  207. }
  208.  
  209. /*------------------------------------------------*/
  210. /* Display whichever values the user wanted to         */
  211. /* display, as indicated by display flags.                */
  212. /*------------------------------------------------*/
  213. int process(time_t * ptim, struct STAT_STRUCT * pStats, int version){
  214.     static int first=1;
  215.     struct tm * ptm;
  216.     long residue;
  217.     ptm = gmtime(ptim);
  218.     if (first){
  219.         fprintf(stdout, "Broadcast activity %02d/%02d/%02d - ", ptm->tm_mday, ptm->tm_mon+1, ptm->tm_year);
  220.  
  221.         if (fCmdmode)
  222.             fprintf(stdout, "(Commands)\n");
  223.         else if (fBytemode)
  224.             fprintf(stdout, "(Bytes)\n");
  225.         else if (fDirmode)
  226.             fprintf(stdout, "(Directories)\n");
  227.  
  228.         fprintf(stdout, "Log is version %d.\n",version);
  229.  
  230.         if (fBytemode){
  231.             if (fFixed)
  232.                 fprintf(stdout, "HH:MM   Req'd     Tx'd    OvrWr     TooOld    Ended    Fopen   PFHerr   EOF\n");
  233.             else
  234.                 fprintf(stdout, "HH:MM   Req'd     Tx'd    Ended    Fopen   PFHerr\n");
  235.         }
  236.         else if (fCmdmode){
  237.             fprintf(stdout, "HH:MM Start  Fill   End  Long  NO-1  NO-2  NO-3  Stations\n");
  238.         }
  239.         else if (fDirmode){
  240.             fprintf(stdout, "HH:MM Requests  Bytes Tx'd\n");
  241.         }
  242.         first = 0;
  243.     }
  244.     fprintf(stdout, "%02d:%02d ", ptm->tm_hour, ptm->tm_min);
  245.     if (fCmdmode){
  246.         fprintf(stdout, "% 5ld ", pStats->nStartFile);
  247.         fprintf(stdout, "% 5ld ", pStats->nHolefills);
  248.         fprintf(stdout, "% 5ld ", pStats->nEndFile);
  249.         fprintf(stdout, "% 5d ", pStats->nLongFile);
  250.         fprintf(stdout, "% 5ld ", pStats->nNoFile);
  251.         fprintf(stdout, "% 5ld ", pStats->nNoRoom);
  252.         fprintf(stdout, "% 5ld ", pStats->nNotOK);
  253.         fprintf(stdout, "% 5d", pStats->nNewStns);
  254.     }
  255.     else if (fBytemode){
  256.         fprintf(stdout, "%8ld ", pStats->nbRequested);
  257.         fprintf(stdout, "%8ld ", pStats->nbTransmitted);
  258.         if (fFixed) {
  259.             fprintf(stdout, "%10lu ", pStats->nbOverwrite);
  260.             fprintf(stdout, "%8ld ", pStats->nbUnfresh);
  261.         }
  262.         fprintf(stdout, "%8ld ", pStats->nbEnd);
  263.         fprintf(stdout, "%8ld ", pStats->nbFopenErr);
  264.         fprintf(stdout, "%8ld ", pStats->nbPfhErr);
  265.         fprintf(stdout, "%8ld ", pStats->nbEof);
  266.         residue = pStats->nbRequested - (pStats->nbTransmitted +
  267.                             pStats->nbOverwrite + pStats->nbUnfresh +
  268.                             pStats->nbEnd + pStats->nbFopenErr + pStats->nbPfhErr +
  269.                             pStats->nbEof);
  270.         if (fFixed)
  271.             fprintf(stdout, " (%7ld)", residue);
  272.  
  273.     }
  274.     else if (fDirmode){
  275.         fprintf(stdout, "% 5ld  ", pStats->nDirReqs);
  276.         fprintf(stdout, "% 8ld ", pStats->nbDirTxd);
  277.     }
  278.     fprintf(stdout, "\n");
  279.  
  280.   return 1;
  281. }
  282.  
  283. /*----------------------------------------------------------------------*/
  284. /* Checks to see if this was one of the early logs which will have bad    */
  285. /* total byte counts in it.                                                                                            */
  286. /* UO-22 files from 6 March 1992 are OK.                                                                */
  287. /* UoSAT-3 files have yet to be fixed 15 April 1992.                                        */
  288. /*----------------------------------------------------------------------*/
  289. int IsNewLog(time_t tim){
  290.     time_t TimFix;
  291.     struct tm Tm;
  292.     memset(&Tm, 0, sizeof(Tm));
  293.     Tm.tm_year = 92;
  294.     Tm.tm_mon = 2;
  295.     Tm.tm_mday = 6;
  296.     TimFix = mktime(&Tm);
  297.     if (tim > TimFix)
  298.         return(TRUE);
  299.     else
  300.         return(FALSE);
  301. }
  302.